package com.usian.user.service.ImpI;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.xml.internal.dtdparser.DTDParser;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.mapper.ApUserFanMapper;
import com.usian.user.mapper.ApUserFollowMapper;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.service.ApUserRelationService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {

    @Autowired
    private ApAuthorFeign apAuthorFeign;

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    @Autowired
    private ApUserFanMapper apUserFanMapper;


    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //1:校验参数
        if (dto.getOperation()<0||dto.getOperation()>1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2:远程调用 获取作者信息
        ApAuthor author = apAuthorFeign.findById(dto.getAuthorId());
        if (author==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"关注的作者不存在");
        }
        //3:操作关注 0 和 取消关注 1
        ApUser user = AppThreadLocalUtils.getUser();
        if (dto.getOperation()==0){
            //关注 本地线程保存的user
            if (user==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"数据不存在");
            }
            //查看关注的用户是否存在
            ApUser followUser = apUserMapper.selectById(author.getUserId());
            if (followUser==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"我不存在");
            }

            //ap_User_Follow 录入数据
            List<ApUserFollow> followList = apUserFollowMapper.findByUserAndFollow(user.getId(), dto.getAuthorId());
            if (followList.size()==0){
                //保存app用户关注信息
                ApUserFollow apUserFollow = new ApUserFollow();
                apUserFollow.setUserId(user.getId());
                apUserFollow.setFollowId(dto.getAuthorId());
                apUserFollow.setFollowName(author.getName());
                apUserFollow.setCreatedTime(new Date());
                apUserFollow.setIsNotice(true);
                apUserFollow.setLevel((short)1);
                apUserFollowMapper.insert(apUserFollow);

                //保存粉丝表查询 去重 有多余的删掉
                List<ApUserFan> userFanList = apUserFanMapper.findByUserIdAndFansId(dto.getAuthorId(), user.getId());
                if (userFanList.size()==0){
                    //保存app用户粉丝信息
                        ApUserFan apUserFan = new ApUserFan();
                        apUserFan.setUserId(dto.getAuthorId());
                        apUserFan.setFansId(user.getId().longValue());
                        apUserFan.setFansName(followUser.getName());
                        apUserFan.setLevel((short)0);
                        apUserFan.setIsDisplay(true);
                        apUserFan.setIsShieldLetter(false);
                        apUserFan.setIsShieldComment(false);
                        apUserFan.setCreatedTime(new Date());
                        apUserFanMapper.insert(apUserFan);
                }
                //关注成功后发送消息 在行为服务中保存获取后的行为
                FollowBehaviorDto dto1=new FollowBehaviorDto();
                dto1.setArticleId(dto.getArticleId());
                dto1.setFollowId(dto.getAuthorId());
                dto1.setUserId(user.getId());
                kafkaTemplate.send("follow_topic", JSON.toJSONString(dto1));
            }else {
                //已关注
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已关注");
            }
        }else if (dto.getOperation()==1){
            //取消关注 删除ap_user_follow  ap_user_fans
            List<ApUserFollow> followList = apUserFollowMapper.findByUserAndFollow(user.getId(), dto.getAuthorId());
            if (followList.size()>0){
                apUserFollowMapper.deleteByUserIdAndFollowId(user.getId(), dto.getAuthorId());
            }
            List<ApUserFan> userFanList = apUserFanMapper.findByUserIdAndFansId(dto.getAuthorId(), user.getId());
            if (userFanList.size()==0){
                apUserFanMapper.deleteByUserIdAndFansId(user.getId(),dto.getAuthorId());
            }
        }
        return null;
    }
}
